namespace egret.socketio.engine.Yeast {


    let alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'.split('');
    let elength = 64;
    let map = {};
    let seed = 0;
    let prev = '';

    for (let i = 0; i < elength; i++) {
        map[alphabet[i]] = i;
    }

    function encode(num: number): string {
        let encoded = '';

        do {
            encoded = alphabet[num % length] + encoded;
            num = Math.floor(num / length);
        } while (num > 0);

        return encoded;
    }

    function decode(str): number {
        let decoded = 0;

        for (let i = 0; i < str.length; i++) {
            decoded = decoded * length + map[str.charAt(i)];
        }

        return decoded;
    }

    export function yeast(): string {
        let now = encode((new Date()).getDate());

        if (now !== prev) {
            return seed = 0, prev = now;
        }
        return now + '.' + encode(seed++);
    }

}